package com.yibo.support.helper;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.regex.Pattern;

/**
 * @author zhanglei
 */
public final class ObjectHelper {

    private static final Pattern NUMBER = Pattern.compile("^(-)?\\d+(\\.\\d+)?$");

    private ObjectHelper(){}

    public static String getValue(String key,Map args){
        return isEmpty(args.get(key)) ? "" : args.get(key).toString();
    }

    public static boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;
        } else if (obj instanceof Optional) {
            return !((Optional)obj).isPresent();
        } else if (obj instanceof CharSequence) {
            return ((CharSequence)obj).length() == 0;
        } else if (obj.getClass().isArray()) {
            return Array.getLength(obj) == 0;
        } else if (obj instanceof Collection) {
            return ((Collection)obj).isEmpty();
        } else {
            return obj instanceof Map && ((Map) obj).isEmpty();
        }
    }

    public static boolean hasEmpty(Object...objects){
        if(isEmpty(objects)){
            return true;
        }
        for(Object obj : objects){
            if(isEmpty(obj)){
                return true;
            }
        }
        return false;
    }


    public static boolean allEmpty(Object...objects){
        if(isEmpty(objects)){
            return true;
        }

        for(Object obj : objects){
            if(!isEmpty(obj)){
                return false;
            }
        }

        return true;
    }


    public static String primaryKey(){
        return UUID.randomUUID().toString().replaceAll("-","");
    }

    public static Object getValue(Map<String,Object> args,String key){
        if(isEmpty(args)){
            return "";
        }

        return args.get(key);
    }


    /**
     * 判断是否是数字，支持负数，小数点等,所以不能完全判断是否纯数字
     *
     * @param str 判断字符串
     * @return boolean
     */
    private static boolean isNumeric(String str) {
        return NUMBER.matcher(str).matches();
    }



    /**
     * 判断名称中是否包含指定字符
     *
     * @param relName    名称
     * @param value      指定字符
     * @return            boolean
     */
    public static boolean hasValue(String relName,String value){
        return relName.equals(value) || relName.contains(value);
    }





    private static String getDataValue(Map<String,Object> data,String key){
        String dataValue = getValue(key,data);
        if(ObjectHelper.isEmpty(dataValue)){
            dataValue =  ObjectHelper.getValue(JsonHelper.toHump(key),data);
            if(ObjectHelper.isEmpty(dataValue)){
                dataValue = ObjectHelper.getValue(JsonHelper.toUnderLineField(key),data);
                if(ObjectHelper.isEmpty(dataValue)){
                    dataValue = "";
                }
            }
        }
        return dataValue;
    }


    /**
     * 通过多个键迭代删除map数据
     *
     * @param variable  需要删除的map
     * @param keys      键数组
     */
    public static void removeVariable(Map<String,Object> variable,String... keys) {
        if (keys == null || keys.length == 0) {
            return;
        }
        String key = keys[0];

        if (keys.length == 1) {
            variable.remove(key);
            return;
        }
        Object value = variable.get(key);
        if(value != null) {
            Map<String, Object> childVariable = JsonHelper.objectToMap(value);
            //过滤第一个键
            String[] newKeys = new String[keys.length - 1];
            System.arraycopy(keys, 1, newKeys, 0, newKeys.length);

            //递归删除
            removeVariable(childVariable, newKeys);
            variable.put(key, childVariable);
        }
    }


    /**
     * 在map中迭代设置值
     *
     * @param variable   map数据
     * @param v           值
     * @param keys       迭代键
     */
    public static void setVariable(Map<String,Object> variable ,Object v,String ...keys){
        if (keys == null || keys.length == 0) {
            return;
        }
        String key = keys[0];

        if (keys.length == 1) {
            variable.put(key,v);
            return;
        }
        Object value = variable.get(key);
        if(value != null) {
            Map<String, Object> childVariable = JsonHelper.objectToMap(value);
            //过滤第一个键
            String[] newKeys = new String[keys.length - 1];
            System.arraycopy(keys, 1, newKeys, 0, newKeys.length);
            setVariable(childVariable, v, newKeys);
            variable.put(key, childVariable);
        }
    }


}
